package com.example.mybatisplus.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mybatisplus.common.utls.SessionUtils;
import com.example.mybatisplus.mapper.UserAccountMapper;
import com.example.mybatisplus.model.domain.UserAccount;
import com.example.mybatisplus.model.dto.PageDTO;
import com.example.mybatisplus.service.UserAccountService;

/**
 * <p>
 * 这是用户账户表 服务实现类
 * </p>
 *
 * @author cft
 * @since 2022-06-24
 */
@Service
public class UserAccountServiceImpl extends ServiceImpl<UserAccountMapper, UserAccount> implements UserAccountService {

	//用户登录接口实现
	@Override
	public UserAccount login(UserAccount userAccount) {
		QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
		wrapper.eq("user_id", userAccount.getUserId()).eq("password", userAccount.getPassword());
		UserAccount one = super.getOne(wrapper);
		return one;
	}


	@Autowired
	UserAccountService userAccountService;

	/* 
	 * author:cft
	 * function:实现已登录用户的密码修改
	 */
	@Override
	public UserAccount changePassword(String oldPassword, String newPassword) {
		if (oldPassword.equals(SessionUtils.getCurrentUserInfo().getPassword())) {
			UserAccount user = SessionUtils.getCurrentUserInfo();
			user.setPassword(newPassword);
			userAccountService.updateById(user);
			return user;
		} else
			return null;
	}
	
	/* 
	 * author:cft
	 * function:实现中心管理员查询咨询师的信息列表
	 */
	@Override
	public Page<UserAccount> getConInfoList(PageDTO pageDTO, UserAccount userAccount) {
		Page<UserAccount> page = new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize());
		QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
		if (userAccount.getUserType() != null && userAccount.getUserType().equals("咨询师"))
			queryWrapper.eq("user_type", userAccount.getUserType());
		if (userAccount.getUserId() != null)
			queryWrapper.eq("user_id", userAccount.getUserId());
		if (StringUtils.isNotBlank(userAccount.getName()))
			queryWrapper.eq("name", userAccount.getName());
		page = super.page(page, queryWrapper);
		return page;
	}

	/* 
	 * author:cft
	 * function:实现修改咨询师个人信息
	 */
	@Override
	public void alterConInfo(UserAccount user) {
		QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_id", user.getUserId());
		userAccountService.update(queryWrapper);
	}

	/* 
	 * author:tqq
	 * function:通过学生姓名查询
	 */
	@Override
	public UserAccount selectByName(String teacherName) {
		QueryWrapper<UserAccount> wrapper = new QueryWrapper<>();
		wrapper.eq("name", teacherName);
		UserAccount userAccount = userAccountService.getOne(wrapper);
		return userAccount;
	}

	
	/* 
	 * author:cft
	 * function:获得所有咨询师的姓名和id
	 */
	@Override
	public List<UserAccount> getAllConNameAndId() {
		QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_type", "咨询师").select("user_id", "name");
		return userAccountService.list(queryWrapper);
	}

	/* 
	 * author:cft
	 * function:得到所有初访员的姓名和id
	 */
	@Override
	public List<UserAccount> getAllFViewerNameAndId() {
		QueryWrapper<UserAccount> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("user_type", "初访员").select("user_id", "name");
		return userAccountService.list(queryWrapper);
	}

	/*
	 * author:tqq
	 * 实现通过id查询userName
	 * */
	@Override
	public String getUserName(Long id) {
		UserAccount userAccount = this.getById(id);
		return userAccount.getName();
	}

}
